<?php

//调货申请被接受处理模块，Input = trans_id，Output = 入库状态或null
header("content-type:application/json;charset=utf-8");

include_once "0.1conn.php"; //连接数据库

//调货单id
$trans_id = $_POST['trans_id'];
//获取shop_id
session_start();
$trans_detail = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM transfer_record WHERE id = $trans_id"));
$ask_shop = $trans_detail['ask_shop'];
$ans_shop = $trans_detail['ans_shop'];
$trans_num = $trans_detail['num'];
$trans_status = $trans_detail['status'];
$good_id = $trans_detail['good_id'];

if ($trans_status == 'yes') {
    //店里本来就有这种衣服
    if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM clothes WHERE shop_id = '$ask_shop' AND good_id = '$good_id'"))) {
        mysqli_query($conn, "UPDATE clothes SET num = num + $trans_num WHERE shop_id = '$ask_shop' AND good_id = '$good_id'");
    } else {
        $cloth_detail = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM clothes WHERE shop_id = '$ans_shop' AND good_id = '$good_id'"));
        $style = $cloth_detail['style'];
        $color = $cloth_detail['color'];
        $size = $cloth_detail['size'];
        $single_price = $cloth_detail['single_price'];
        $discount = $cloth_detail['discount'];

        mysqli_query($conn, "INSERT INTO clothes (good_id,num,style,color,size,single_price,discount,shop_id) 
            VALUES ('$good_id','$trans_num','$style', '$color', '$size', '$single_price', '$discount','$ask_shop')");
    }
    mysqli_query($conn, "DELETE FROM transfer_record WHERE id = '$trans_id'");
} else {                //wait取消和no删除效果一样
    mysqli_query($conn, "DELETE FROM transfer_record WHERE id = '$trans_id'");
}
//断开连接
mysqli_close($conn);
